﻿using Microsoft.Extensions.Caching.Distributed;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using UpperComputer.Application;
using UpperComputer.WPF.Entry.Common;

namespace UpperComputer.WPF.Entry.ViewModels;

public class UserRoleSelectViewModel : BaseViewModel
{
    private readonly BaseRoleService _roleService;

    public readonly string _code;

    public ObservableCollection<BaseRoleResultDto> Roles { get; set; } = new ObservableCollection<BaseRoleResultDto>();

    public UserRoleSelectViewModel(BaseRoleService roleService)
    {
        _roleService = roleService;
        _code = DistributedCache.Get<string>(Constant.CACHE_KEY_MENU_CODE);
    }

    protected override async Task LoadAsync(object parameter, CancellationToken cancellationToken)
    {
        YFurion.Application.PagedResultDto<BaseRoleResultDto> rolePaged = await _roleService.GetAll(new BaseRoleResultRequestDto(), cancellationToken);
        foreach (BaseRoleResultDto item in rolePaged.Items)
        {
            Roles.Add(item);
        }
    }
}